import { queryPage, queryInfoById, create, remove, update } from './service';

import { queryDictList } from '@/services/dict';

export default {
  namespace: 'build_floor_area',

  state: {
    list: { data: [] },
    details: [],
    cond: {}
  },

  effects: {
    *initCond(_, { put }) {
      let payload = _.payload;
      yield put({ type: 'setCond', payload });
      yield put({ type: 'queryPage' })
    },
    *insert(_, { put }) {
      yield put({ type: 'insertCond' });
    },
    *updateCond({ payload }, { put }) {
      yield put({ type: 'setCond', payload });
      yield put({ type: 'queryPage' })
    },
    *queryPage({ payload = { pageNum: 1 } }, { call, put, select }) {
      const cond = yield select(state => state.build_floor_area.cond);
      const params = { ...payload, ...cond };
      if (!params.pageSize) params.pageSize = 10;
      const { list = [], total } = yield call(queryPage, params);
      yield put({ type: 'setPage', payload: { list, total: +total } })
    },
    *queryDict({ payload }, { call, put }) {
      const dict = yield call(queryDictList, payload);
      yield put({ type: 'setDict', payload: dict })
    },
    *queryInfoById({ payload }, { call, put }) {
      const response = yield call(queryInfoById, payload);
      yield put({ type: 'setInfo', payload: response })
    },
    *queryEditInfoById({ payload }, { call, put }) {
      const response = yield call(queryInfoById, payload.id);
      const items = {};

      payload.keys.forEach(item => {
        items[item] = response[item]
      })
      yield put({ type: 'setEditInfo', payload: items })
    },
    *create({ payload }, { call, put }) {
      yield call(create, payload);
      yield put({ type: 'queryPage', payload: { pageNum: 1, floorId: payload.floorId } })
    },
    *update({ payload }, { call, put }) {
      yield call(update, payload);
      yield put({ type: 'queryPage', payload: { floorId: payload.floorId, pageNum: 1, pageSize: 10 } });
    },
    *remove({ payload }, { call, put }) {
      yield call(remove, payload.id)
      yield put({ type: 'queryPage', payload: { floorId: payload.floorId, pageNum: 1, pageSize: 10 } });
    },
    *init({ payload }, { put }) {
      yield put({ type: 'upload/setImgValue', payload: { url: payload.url } });
      yield put({ type: 'queryDict', payload: payload.dict });
    }
  },

  reducers: {
    setCond(state, action) {
      return {
        ...state,
        cond: action.payload
      }
    },
    insertCond(state) {
      return {
        ...state,
        cond: {}
      }
    },
    setDict(state, action) {
      return {
        ...state,
        dict: action.payload
      }
    },
    setPage(state, action) {
      return {
        ...state,
        list: {
          data: action.payload.list,
          dataCnt: action.payload.total
        }
      }
    },
    setInfo(state, action) {
      return {
        ...state,
        details: action.payload
      }
    },
    setEditInfo(state, action) {
      return {
        ...state,
        editDetails: action.payload
      }
    }
  }
}
